package me.zxk.qwert_poetry.service;

import me.zxk.qwert_poetry.dao.Tag;
import me.zxk.qwert_poetry.dao.TagRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TagServiceImpl implements TagService {
    @Autowired
    private TagRepository tagRepository;

    @Override
    public Page<Tag> listTags(int page, int size) {
        return tagRepository.findAll(PageRequest.of(page, size));
    }

    @Override
    public Tag getTagById(int id) {
        return tagRepository.findById(id).orElse(null);
    }

    @Override
    public Tag getTagByName(String name) {
        return tagRepository.findByName(name).orElse(null);
    }

    @Override
    public Tag addTag(Tag tag) {
        return tagRepository.save(tag);
    }

    @Override
    public Tag editTag(Tag tag) {
        return tagRepository.save(tag);
    }

    @Override
    public void deleteTag(int id) {
        tagRepository.deleteById(id);
    }
}
